package me.raih.config;

import me.raih.interceptor.AdminHandlerInterceptor;
import me.raih.interceptor.SalesHandlerInterceptor;
import me.raih.interceptor.UserHandlerInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author Raih
 * @Time 2020/11/27  13:43
 *
 * 关于web的一些配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //空页面就去登录页面
        registry.addViewController("/user").setViewName("/user/login");
        registry.addViewController("/user/index").setViewName("/user/index");
        registry.addViewController("/index").setViewName("/user/index");
        registry.addViewController("/user/login.html").setViewName("/user/login");
        registry.addViewController("/user/register.html").setViewName("/user/register");
    }

    /**
     *  后台 登录拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         * 商家登录拦截器
         */
        registry.addInterceptor(new AdminHandlerInterceptor())     //注册拦截器
                .addPathPatterns("/admin/**") //拦截谁
                .addPathPatterns("/admin")
                .excludePathPatterns("/admin/login","/admin/adminLoginCheck");

        /**
         * 用户登录拦截器
         */
        registry.addInterceptor(new UserHandlerInterceptor())
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/findCategory")  //菜单不要拦截
                .excludePathPatterns("/user/index","/user/","/user/index.html")
                .excludePathPatterns("/user/login","/user/userLoginCheck","/user/login.html","/user/logOut")
                .excludePathPatterns("/user/register","/user/register.html","/user/userRegisterCheck")
                .excludePathPatterns("/user/emailCheck");

        /**
         * 售货员登录拦截器
         */
        registry.addInterceptor(new SalesHandlerInterceptor())
                .addPathPatterns("/sales/**")
                .excludePathPatterns("/sales/salesLoginCheck");
    }


}
